package net.pws.oos.web;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletResponse;

import net.pws.common.util.StringUtils;
import net.pws.common.jackson.JsonUtils;
import net.pws.common.persistence.EntityManager;
import net.pws.oos.biz.BizException;
import net.pws.oos.biz.model.Message;
import net.pws.oos.biz.query.MessageFinder;
import net.pws.oos.biz.query.parameter.MessageQueryParameter;
import net.pws.oos.biz.service.MessageService;
import net.pws.oos.security.SecurityUtils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.ServletContextAware;

@Controller
@RequestMapping("/biz/message")
public class MessageAction implements ServletContextAware {
    
    @Autowired
    private EntityManager entityManager;
    
    @Autowired
    private MessageFinder messageFinder;
    
    @Autowired
    private MessageService messageService;
    
    private ServletContext context;
    
    public void setServletContext(ServletContext context) {
        this.context = context;
    }
    
    @RequestMapping("/list.do")
    public String listPage() {
        return "message/list";
    }
    
    @RequestMapping("/read.do")
    public String read(String id, ModelMap map, HttpServletResponse response) {
        if (StringUtils.isBlank(id)) {
            throw new BizException("错误的参数！");
        }
        
        Message message = entityManager.find(Message.class, id);
        if (message == null) {
            throw new BizException("错误的参数！");
        }
        
        if (!message.isRead()) {
            messageService.read(message);
        }
        
        String url = message.getUrl();
        if(StringUtils.isNotBlank(url)) {
            if (!url.startsWith("http")) {
                url = context.getContextPath() + url;
            }
            try {
                response.sendRedirect(url);
            }
            catch (IOException e) {
                throw new BizException("系统跳转错误！", e);
            }
            return null;
        }
        
        map.put("detail", message);
        return "message/read";
    }
    
    @RequestMapping("/count.json")
    @ResponseBody
    public Object countDatas(MessageQueryParameter queryParameter) {
        queryParameter = initQueryParameters(queryParameter);
        return JsonUtils.succeedMap(messageFinder.countMessage(queryParameter));
    }
    
    @RequestMapping("/list.json")
    @ResponseBody
    public Object listDatas(MessageQueryParameter queryParameter) {
        queryParameter = initQueryParameters(queryParameter);
        List<Message> models = messageFinder.listMessage(queryParameter);
        return JsonUtils.succeedMap(models);
    }
    
    @RequestMapping("/news.json")
    @ResponseBody
    public Object findNews() {
        MessageQueryParameter queryParameter = initQueryParameters(null);
        queryParameter.setRead(Boolean.FALSE);
        queryParameter.setLimit(3);
        
        Map<String, Object> map = new HashMap<String, Object>();
        int count = messageFinder.countMessage(queryParameter);
        map.put("count", count);
        if (count > 0) {
            List<Message> models = messageFinder.listMessage(queryParameter);
            map.put("list", models);
        }
        
        return JsonUtils.succeedMap(map);
    }
    
    private MessageQueryParameter initQueryParameters(MessageQueryParameter queryParameter) {
        if (queryParameter == null) {
            queryParameter = new MessageQueryParameter();
        }
        queryParameter.setUserId(SecurityUtils.currentUserId());
        return queryParameter;
    }
    
}
